package com.sduept.nwld.dataserver.manager.document;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sduept.bigdata.document.core.CommonDocumentProperties;
import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.dataserver.model.AppDocument;
import com.sduept.nwld.dataserver.model.AppDocumentDir;

@Service
public class DocumentDirManager {
	
	@Autowired
	private SuperDAO sdao;
	@Autowired
	private CommonDocumentProperties pros;
	/**
	 * 获取文档根目录
	 * @return
	 */
	public List<AppDocumentDir> findAllRootDocumentDir() {
		return  sdao.findByCondition("pid is null",null, "order by orderNo",null,AppDocumentDir.class);
	}
	
	/**
	 * 获取所有的文档目录
	 * @return
	 */
	public List<AppDocumentDir> findAllDocumentDir() {
		return sdao.findAll(AppDocumentDir.class);
	}
	public AppDocumentDir findAppDocumentDirById(String id) {
		return sdao.findById(id, AppDocumentDir.class);
	}
	/**
	 * 根据pid获取所有的文档目录
	 * @return
	 */
	public List<AppDocumentDir> findDocumentDirByPid(String pid) {
		List<AppDocumentDir> result=new ArrayList<>();
		if(pid==null){
			result=sdao.findByCondition("pid is null",null, "order by orderNo",null,AppDocumentDir.class);
		}else{
			result=sdao.findByPid(pid,"order by orderNo", AppDocumentDir.class);
		}
		return result;
	}
	/**
	 * 根据name获取文档目录
	 * @return
	 */
	public AppDocumentDir findDocumentDirByName(String dirName) {
		AppDocumentDir result=null;
		if(StringUtils.isNotEmpty(dirName)){
			List<AppDocumentDir> dirs=sdao.findByCondition(AppDocumentDir.class, "name=?", new Object[]{dirName});
		    if(dirs.size()>0){
		    	result=dirs.get(0);
		    }
		    
		}
		return result;
	}
	/**
	 * 创建或编辑一个目录
	 * @param dir
	 * @return
	 */
	public AppDocumentDir createOrUpdateAppDocumentDir(AppDocumentDir dir) {
		return sdao.createOrUpdate(dir, AppDocumentDir.class);
	}
	
	/**
	 * 根据选择的目录id删除某文档目录下所有的目录和文档
	 * @param id
	 */
	public void deleteDirs(AppDocumentDir selectedDir) {
		List<AppDocumentDir> dirs=sdao.findByPid(selectedDir.getId(), AppDocumentDir.class);
		if(dirs.size()>0){
			for (AppDocumentDir appDocumentDir : dirs) {
				deleteDirs(appDocumentDir);
			}
		}
		List<AppDocument> docs=findDocumensByPid(selectedDir.getId());
		for(AppDocument doc:docs){
			deleteAPPDocument(doc);
		}
		sdao.delete(selectedDir.getId(),AppDocumentDir.class);
		
	}
	
	/**
	 * 删除某文档记录
	 * @param id
	 */
	public void deleteAPPDocument(AppDocument doc) {
		String path =doc.getPath();
		if(StringUtils.isNotEmpty(path)){
			File f = new File(pros.getDocumentPath()+path);
			if(f.exists())
				f.delete();
		}
		if(StringUtils.isNotEmpty(doc.getId()))
			sdao.delete(doc.getId(), AppDocument.class);
	}
	/**
	 * 根据id获取某条文档信息
	 * @param id
	 * @return
	 */
	public AppDocument findAppDocumentById(String id) {
		return sdao.findById(id, AppDocument.class);
	}
	/**
	 * 根据目录id获取目录下所有文档
	 * @param oid
	 * @return
	 */
	public List<AppDocument> findDocumensByPid(String pid) {
		//return sdao.findByPid(pid, AppDocument.class);
		return sdao.findByPid(pid, "order by orderNo", AppDocument.class);
	}
	/**
	 * 根据目录id和文档的年月获取文档
	 * @param pid,month,year
	 * @return
	 */
	public List<AppDocument> findDocumentsByPidAndDate(String pid,Integer month,Integer year) {
		List<Object> whereArgs=new ArrayList<Object>();
		StringBuilder whereCase=new StringBuilder();
		if(StringUtils.isEmpty(pid)){
			whereCase.append("pid is null");
		}else{
			whereCase.append("pid=?");
			whereArgs.add(pid);
		}
		if(month==null){
			whereCase.append(" and month is null");
		}else{
			whereCase.append(" and month=?");
			whereArgs.add(month);
		}
		if(year==null){
			whereCase.append(" and year is null");
		}else{
			whereCase.append(" and year=?");
			whereArgs.add(year);
		}
		//return sdao.findByCondition(AppDocument.class, whereCase.toString(), whereArgs.toArray());
		return sdao.findByCondition(whereCase.toString(), whereArgs.toArray(), "order by orderNo", null, AppDocument.class);
	}
	/**
	 * 根据目录id获取全年的文档
	 * @param pid,year
	 * @return
	 */
	public List<AppDocument> findDocumensByPidAndDate(String pid,Integer year) {
		List<Object> whereArgs=new ArrayList<Object>();
		StringBuilder whereCase=new StringBuilder();
		if(StringUtils.isEmpty(pid)){
			whereCase.append("pid is null");
		}else{
			whereCase.append("pid=?");
			whereArgs.add(pid);
		}
		if(year==null){
			whereCase.append(" and year is null");
		}else{
			whereCase.append(" and year=?");
			whereArgs.add(year);
		}
		//return sdao.findByCondition(AppDocument.class, whereCase.toString(), whereArgs.toArray());
		return sdao.findByCondition(whereCase.toString(), whereArgs.toArray(), "order by orderNo", null, AppDocument.class);
	}
	/**
	 * 创建或编辑一条文档信息
	 * @param doc
	 * @return
	 */
	public AppDocument createOrUpdateRecordDocument(AppDocument doc) {
		return sdao.createOrUpdate(doc, AppDocument.class);
	}
	
	/**
	 * 根据目录名称获取文档
	 * @param dirName,dirPid
	 * @return
	 */
	public List<AppDocument> getDocumentByDirName(String name,String pid){
		List<AppDocument> result=new ArrayList<AppDocument>();
		if(StringUtils.isNotEmpty(name)){
			String whereCase = pid==null?"name=? and pid is null":"name=? and pid =?";
			Object[] whereArgs=pid==null?new Object[]{name}:new Object[]{name,pid};
			List<AppDocumentDir> dirs=sdao.findByCondition(whereCase, whereArgs, AppDocumentDir.class);
			if(dirs.size()>0){
				result=findDocumensByPid(dirs.get(0).getId());
			}
		}
		return result;
	}
	/**
	 * 根据目录id获取该目录下的文档
	 * @param dirName,dirPid
	 * @return
	 */
	public List<AppDocument> getDocsByPid(String pid){
		String whereCase = pid==null?"pid is null":"pid =?";
		Object[] whereArgs=pid==null?null:new Object[]{pid};
		//List<AppDocument> docs=sdao.findByCondition(whereCase, whereArgs, AppDocument.class);
		return sdao.findByCondition(whereCase, whereArgs, "order by orderNo", null, AppDocument.class);
	}
	
	/**
	 * 根据目录名称年月获取文档
	 * @param dirName,dirPid,month,year
	 * @return
	 */
	public List<AppDocument> getDocumentByDirNameAndDate(String name,String pid,Integer month,Integer year){
		List<AppDocument> result=new ArrayList<AppDocument>();
		if(StringUtils.isNotEmpty(name)){
			String whereCase = pid==null?"name=? and pid is null":"name=? and pid =?";
			Object[] whereArgs=pid==null?new Object[]{name}:new Object[]{name,pid};
			List<AppDocumentDir> dirs=sdao.findByCondition(whereCase, whereArgs, AppDocumentDir.class);
			if(dirs.size()>0){
				result=findDocumentsByPidAndDate(dirs.get(0).getId(),month,year);
			}
		}
		return result;
	}
	/**
	 * 根据目录名称获取某一年的文档
	 * @param dirName,dirPid,year
	 * @return
	 */
	public List<AppDocument> getDocumentByDirNameAndDate(String name,String pid,Integer year){
		List<AppDocument> result=new ArrayList<AppDocument>();
		if(StringUtils.isNotEmpty(name)){
			String whereCase = pid==null?"name=? and pid is null":"name=? and pid =?";
			Object[] whereArgs=pid==null?new Object[]{name}:new Object[]{name,pid};
			List<AppDocumentDir> dirs=sdao.findByCondition(whereCase, whereArgs, AppDocumentDir.class);
			if(dirs.size()>0){
				result=findDocumensByPidAndDate(dirs.get(0).getId(),year);
			}
		}
		return result;
	}
	/**
	 * 根据文档名称，文档所属目录、年、月查找该文档
	 * @param fileName,docPid,month,year
	 * @return
	 */
	public List<AppDocument> getDocumentByFileNameAndDate(String fileName,String pid,Integer month,Integer year){
		List<AppDocument> result=new ArrayList<AppDocument>();
		if(StringUtils.isNotEmpty(fileName)){
			List<Object> whereArgs=new ArrayList<Object>();
			whereArgs.add(fileName);
			StringBuilder whereCase=new StringBuilder("name=?");
			if(StringUtils.isEmpty(pid)){
				whereCase.append(" and pid is null");
			}else{
				whereCase.append(" and pid=?");
				whereArgs.add(pid);
			}
			if(month==null){
				whereCase.append(" and month is null");
			}else{
				whereCase.append(" and month=?");
				whereArgs.add(month);
			}
			if(year==null){
				whereCase.append(" and year is null");
			}else{
				whereCase.append(" and year=?");
				whereArgs.add(year);
			}
			result=sdao.findByCondition(AppDocument.class, whereCase.toString(), whereArgs.toArray());
		}
		return result;
	}
	/**
	 * 根据文档名称，文档所属目录、年查找该文档
	 * @param fileName,docPid,year
	 * @return
	 */
	public List<AppDocument> getDocumentByFileNameAndYear(String fileName,String pid,Integer year){
		List<AppDocument> result=new ArrayList<AppDocument>();
		if(StringUtils.isNotEmpty(fileName)){
			List<Object> whereArgs=new ArrayList<Object>();
			whereArgs.add(fileName);
			StringBuilder whereCase=new StringBuilder("name=?");
			if(StringUtils.isEmpty(pid)){
				whereCase.append(" and pid is null");
			}else{
				whereCase.append(" and pid=?");
				whereArgs.add(pid);
			}
			if(year==null){
				whereCase.append(" and year is null");
			}else{
				whereCase.append(" and year=?");
				whereArgs.add(year);
			}
			result=sdao.findByCondition(AppDocument.class, whereCase.toString(), whereArgs.toArray());
		}
		return result;
	}
	/**
	 * 根据文档名称，文档所属目录查找该文档
	 * @param fileName,docPid
	 * @return
	 */
	public List<AppDocument> getDocumentByFileName(String fileName,String pid){
		List<AppDocument> result=new ArrayList<AppDocument>();
		if(StringUtils.isNotEmpty(fileName)){
			List<Object> whereArgs=new ArrayList<Object>();
			whereArgs.add(fileName);
			StringBuilder whereCase=new StringBuilder("name=?");
			if(StringUtils.isEmpty(pid)){
				whereCase.append(" and pid is null");
			}else{
				whereCase.append(" and pid=?");
				whereArgs.add(pid);
			}
			result=sdao.findByCondition(AppDocument.class, whereCase.toString(), whereArgs.toArray());
		}
		return result;
	}
	
	/**
	 * 根据pid获取所有的app文档目录
	 * @return
	 */
	public List<AppDocumentDir> findAppDocumentDirByPid(String pid) {
		List<AppDocumentDir> result=new ArrayList<>();
		if(pid==null){
			result=sdao.findByCondition("pid is null",null, "order by orderNo",null,AppDocumentDir.class);
		}else{
			result=sdao.findByCondition("pid =?",new Object[]{pid}, "order by orderNo",null,AppDocumentDir.class);
		}
		return result;
	}
	
	
	/**
	 * 根据name获取文档目录
	 * @return
	 */
	public AppDocumentDir findAppDocumentDirByName(String dirName) {
		AppDocumentDir result=null;
		if(StringUtils.isNotEmpty(dirName)){
			List<AppDocumentDir> dirs=sdao.findByCondition(AppDocumentDir.class, "name=?", new Object[]{dirName});
		    if(dirs.size()>0){
		    	result=dirs.get(0);
		    }
		    
		}
		return result;
	}
	/**
	 * 获取除文档根目录外其他目录
	 * @return
	 */
	public List<AppDocumentDir> findDirExcludeRootDir() {
		return  sdao.findByCondition(AppDocumentDir.class, "pid is not null");
	}
	/**
	 * 获取所有的文档
	 * @return
	 */
	List<AppDocument> getAllDocs(){
		return sdao.findAll(AppDocument.class);
	}
	/**
	 * 根据目录获取该目录下所有的子目录和每个目录下的文档
	 * */
	public DirDto getAllDirsAndDocsByDir(AppDocumentDir currentDir) {
		DirDto dto=new DirDto();
		if(null!=currentDir) {
			//获取所有的目录和文档
			List<AppDocumentDir> allDirs=findDirExcludeRootDir();
			List<AppDocument> allDocs=getAllDocs();
			dto=getDirDtoByDir(currentDir,allDirs,allDocs);
		}
		return dto;
	}
	private DirDto getDirDtoByDir(AppDocumentDir currentDir,List<AppDocumentDir> allDirs,List<AppDocument> allDocs) {
		DirDto dto=new DirDto();
		if(null!=currentDir) {
			dto.setDir(currentDir);
			String currentDirId=currentDir.getId();
			Iterator<AppDocument> docIterator=allDocs.iterator();
			while(docIterator.hasNext()) {
				AppDocument doc=docIterator.next();
				if(currentDirId.equals(doc.getPid())) {
					dto.getDocs().add(doc);
					docIterator.remove();
				}
			}
			Iterator<AppDocumentDir> dirIterator=allDirs.iterator();
			while(dirIterator.hasNext()) {
				AppDocumentDir subDir=dirIterator.next();
				if(currentDirId.equals(subDir.getPid())) {
					dto.getDirs().add(getDirDtoByDir(subDir,allDirs,allDocs));
				}
			}
			
		}
		return dto;
	}

	//文档目录下的所有子目录以及每个目录下的文档数据模型
		public class DirDto{
			private AppDocumentDir dir;//当前目录
			private List<DirDto> dirs=new ArrayList<>();//该目录下的子目录
			private List<AppDocument> docs=new ArrayList<>();//该目录下的文档
			public List<AppDocument> getDocs() {
				return docs;
			}
			public void setDocs(List<AppDocument> docs) {
				this.docs = docs;
			}
			public AppDocumentDir getDir() {
				return dir;
			}
			public void setDir(AppDocumentDir dir) {
				this.dir = dir;
			}
			public List<DirDto> getDirs() {
				return dirs;
			}
			public void setDirs(List<DirDto> dirs) {
				this.dirs = dirs;
			}
		}
}
